package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.UIManager;

import org.scilab.forge.jlatexmath.TeXIcon;

import fn.Constant;
import fn.Function;
import fn.Latex;
import fn.Variable;

public class InterpolationOutput extends JPanel {
	private static final long serialVersionUID = -940402774745397057L;

	public JLabel lblFx;
	public JScrollPane scrErr;
	private Function p, e;
	public JLabel lblErr;
	public JScrollPane scrollPane_1;
	public JPanel panelFn;
	public JPanel panelErr;

	public static void main(String[] args) throws Exception {
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		JFrame f = new JFrame();
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		InterpolationOutput p = new InterpolationOutput();
		p.setFn(Variable.create());
		p.setErr(Constant.create(1));
		f.getContentPane().add(p);
		f.pack();
	}

	public void setFn(Function f) {
		this.p = f;
		panelFn.repaint();
	}

	public void setErr(Function f) {
		this.e = f;
		panelErr.repaint();
	}

	public InterpolationOutput() {
		init();
	}

	private void init() {
		setPreferredSize(new Dimension(256, 256));
		setFocusable(false);
		setFocusTraversalKeysEnabled(false);
		lblFx = new JLabel("") {
			private static final long serialVersionUID = 2780638449319426443L;

			@Override
			public void paint(Graphics arg0) {
				Latex.draw("P(x) = ", 16, this, arg0, getForeground());
			}

		};
		lblFx.setFocusable(false);
		lblFx.setFocusTraversalKeysEnabled(false);

		scrErr = new JScrollPane();
		scrErr.setFocusable(false);
		scrErr.setFocusTraversalKeysEnabled(false);

		lblErr = new JLabel("") {
			private static final long serialVersionUID = 1666637947133568077L;

			public void paint(Graphics arg0) {
				Latex.draw("E(x) = ", 16, this, arg0, getForeground());
			}
		};
		lblErr.setFocusable(false);
		lblErr.setFocusTraversalKeysEnabled(false);

		scrollPane_1 = new JScrollPane();
		scrollPane_1.setFocusable(false);
		scrollPane_1.setFocusTraversalKeysEnabled(false);
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(groupLayout
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						groupLayout.createSequentialGroup().addComponent(lblFx, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE)
								.addContainerGap(205, Short.MAX_VALUE))
				.addGroup(groupLayout.createSequentialGroup().addComponent(lblErr, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE).addContainerGap())
				.addComponent(scrollPane_1, GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE).addComponent(scrErr, GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(
				groupLayout.createSequentialGroup().addComponent(lblFx, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED).addComponent(scrErr, GroupLayout.PREFERRED_SIZE, 61, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED).addComponent(lblErr, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED).addComponent(scrollPane_1, GroupLayout.PREFERRED_SIZE, 61, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(70, Short.MAX_VALUE)));

		panelFn = new JPanel() {
			private static final long serialVersionUID = 4070530114764691873L;

			@Override
			public void paint(Graphics arg0) {
				super.paint(arg0);
				TeXIcon ti;
				try {
					ti = Latex.create(p, 16);
					ti.setForeground(Color.BLACK);
				} catch (Exception ex) {
					ti = Latex.create(p, 16);
					ti.setForeground(Color.RED.darker());
				}
				ti.paintIcon(this, arg0, 4, 4);
				if (ti.getIconWidth() != getWidth() && ti.getIconHeight() != getHeight()) {
					setSize(ti.getIconWidth(), ti.getIconHeight());
					setPreferredSize(getSize());
				}
			}
		};
		scrErr.setViewportView(panelFn);
		panelFn.setPreferredSize(new Dimension(0, 0));
		panelFn.setFocusable(false);
		panelFn.setFocusTraversalKeysEnabled(false);
		panelFn.setBackground(Color.WHITE);
		panelFn.setAlignmentY(0.0f);
		panelFn.setAlignmentX(0.0f);
		GroupLayout gl_panel_1 = new GroupLayout(panelFn);
		gl_panel_1.setHorizontalGroup(gl_panel_1.createParallelGroup(Alignment.LEADING).addGap(0, 264, Short.MAX_VALUE).addGap(0, 254, Short.MAX_VALUE));
		gl_panel_1.setVerticalGroup(gl_panel_1.createParallelGroup(Alignment.LEADING).addGap(0, 59, Short.MAX_VALUE).addGap(0, 59, Short.MAX_VALUE));
		panelFn.setLayout(gl_panel_1);

		panelErr = new JPanel() {
			private static final long serialVersionUID = 4070530114764691873L;

			@Override
			public void paint(Graphics arg0) {
				try {
					super.paint(arg0);
					if (e == null)
						return;
					TeXIcon ti;
					try {
						ti = Latex.create(e, 16);
						ti.setForeground(Color.BLACK);
					} catch (Exception ex) {
						ti = Latex.create(e, 16);
						ti.setForeground(Color.RED.darker());
					}
					ti.paintIcon(this, arg0, 4, 4);
					if (ti.getIconWidth() != getWidth() && ti.getIconHeight() != getHeight()) {
						setSize(ti.getIconWidth(), ti.getIconHeight());
						setPreferredSize(getSize());
					}
				} catch (Exception e) {}
			}
		};
		panelErr.setPreferredSize(new Dimension(0, 0));
		panelErr.setFocusable(false);
		panelErr.setFocusTraversalKeysEnabled(false);
		panelErr.setBackground(Color.WHITE);
		panelErr.setAlignmentY(0.0f);
		panelErr.setAlignmentX(0.0f);
		scrollPane_1.setViewportView(panelErr);
		GroupLayout gl_panelErr = new GroupLayout(panelErr);
		gl_panelErr.setHorizontalGroup(gl_panelErr.createParallelGroup(Alignment.LEADING).addGap(0, 260, Short.MAX_VALUE).addGap(0, 264, Short.MAX_VALUE));
		gl_panelErr.setVerticalGroup(gl_panelErr.createParallelGroup(Alignment.LEADING).addGap(0, 59, Short.MAX_VALUE).addGap(0, 59, Short.MAX_VALUE));
		panelErr.setLayout(gl_panelErr);
		setLayout(groupLayout);
	}
}
